import { Box3Get, get } from "@ap/util/request";
import * as vscode from "vscode";
import {
  showErrorMessage,
  showInformationMessage,
} from "@ap/util/vscode/message";
import { auth } from "@ap/util/request";
import { updatePackageJson, getPackageJson } from "@ap/util/packageJson";
import { PackageConfig } from "@ap/types/config";
import { getGroupProject, STATIC_URL } from "./getGroupProject";

/**
 * 异步获取扩展地图UI列表数组。
 * @returns 返回承诺，解析为数据项数组。
 */
export async function getBox3MapUiList(): Promise<
  Record<string, any> | undefined
> {
  return getPackageJson<PackageConfig>("map").then(async (packageJson) => {
    if (packageJson) {
      const mapId = packageJson.id;
      if (mapId) {
        const response = await getGroupProject(mapId);
        if (!response) {
          return;
        }
        const requestOptions2 = {
          url: STATIC_URL + response.data.uiTree,
        };
        const response2 = await get<Record<string, any>>(
          requestOptions2,
          "地图UI界面",
        );
        if (!response2) {
          showErrorMessage(`获取地图UI数据失败`);
          return;
        }
        const requestOptions3 = {
          url: "engine/default-screen-id?mapId=" + mapId + "&mode=edit",
        };
        const response3 = await Box3Get(requestOptions3, "地图UI界面");
        if (response3.data) {
          response2[response3.data].default_ui = true;
        }
        return response2;
      }
    }
  });
}
